function OnEvent_Initialize(){
	g_hWMP.settings.autostart = 0;
	function g_hWMP::CurrentPlaylistChange(){
		if(WL_PLAYLIST)
			WL_LoadPlaylist(WL_PLAYLIST.GetControlText("search"));
		}
	function g_hWMP::CurrentItemChange(){
		if(WL_PLAYER){
			g_iInfo = -1;
			MsgPlus.CancelTimer("WL_InfoUpdate");
			WL_InfoUpdate();
			}
		}
	function g_hWMP::PlayStateChange(NewState){
		switch(NewState){
			case WL_STOP:
			case 10:
				if(g_oSettings.NOW_PLAYING)
					WL_SetNowPlaying(0);
				WL_SetButton();
			break;
			case WL_PLAY:
			case WL_PAUSE:
				if(g_oSettings.NOW_PLAYING){
					var oMedia = g_hWMP.currentMedia;
					var aTags = ["%t", "%ab", "%a", "%d", "%s"];
					var aReplace = ["{0}", "{2}", "{1}", oMedia.duration?oMedia.durationString:"Stream", [,, "Paused", "Playing"][NewState]];
					var szSetPSM = g_oSettings.NOW_PLAYING_FORMAT;
					for(i in aTags)
						szSetPSM = szSetPSM.replace(aTags[i], aReplace[i]);
					WL_SetNowPlaying(1, szSetPSM, oMedia.getItemInfo("title"), oMedia.getItemInfo("artist"), oMedia.getItemInfo("WM/AlbumTitle"));
					}
				WL_SetButton();
			break;
			}
		}
	if(Messenger.MyStatus)
		OnEvent_Signin();
	}

function OnEvent_Timer(TimerId){eval(TimerId + "()");}
function OnWLMiniEvent_Destroyed(){WL_PLAYER = null;}
function OnplistEvent_Destroyed(){WL_PLAYLIST = null;}
function OnprefsEvent_Destroyed(){WL_PREFERENCES = null;}
function OnAboutBoxEvent_Destroyed(){WL_ABOUT = null;}

function OnWLMiniEvent_Cancel(PlusWnd){
	MsgPlus.CancelTimer("WL_DataUpdate");
	MsgPlus.CancelTimer("WL_InfoUpdate");
	}

function OnEvent_Signin(){
	g_oSettings = {
		BACKGROUND_IMAGE : "",
		LANGUAGE : "English.txt",
		NOW_PLAYING : true,
		NOW_PLAYING_FORMAT : "%t - %a (%d)",
		PLAYER_SKIN : "WLMini.xml",
		PLAYER_ALPHA : 255,
		PLAYLIST_ALPHA : 255,
		VOLUME : 50,
		LOOP : false,
		SHUFFLE : false
		};
	WL_Settings(WL_LOAD);
	}

function OnEvent_Signout(){
	WL_Settings(WL_SAVE);
	g_hWMP.close();
	}

function OnEvent_Uninitialize(MessengerExit){OnEvent_Signout();}

function OnplistEvent_EditTextChanged(PlusWnd, ControlId){WL_LoadPlaylist(PlusWnd.GetControlText(ControlId));}

function OnplistEvent_LstViewDblClicked(PlusWnd, ControlId, ItemIdx){
	if(ItemIdx>-1)
		WL_Controls(WL_PLAY, PlusWnd.LstView_GetItemData(ControlId, ItemIdx));
	}

function OnplistEvent_CtrlClicked(PlusWnd, ControlId){
	if(ControlId.charAt(0) == "W")
		eval(ControlId + "()");
	}

function OnAboutBoxEvent_CtrlClicked(PlusWnd, ControlId){
	switch(ControlId){
		case "Link1":g_hWMP.launchURL("http://msghelp.net/showthread.php?tid=82096");break;
		case "Link2":g_hWMP.launchURL("http://update.meetcweb.com");break;
		case "Link3":g_hWMP.launchURL("http://zombiesonweed.com");break;
		}
	}

function OnprefsEvent_CtrlClicked(PlusWnd, ControlId){
	switch(ControlId){
		case "BGImg":PlusWnd.SetControlText("BGImg", OpenFileDialog("Select Image", PlusWnd.handle, "Pictures|*.jpg;*.gif;*.bmp;*.png|")[0]);break;
		case "BtnAccept":
			g_oSettings.BACKGROUND_IMAGE = PlusWnd.GetControlText("BGImg");
			g_oSettings.PLAYER_SKIN = PlusWnd.GetControlText("PlayerSkin");
			g_oSettings.LANGUAGE = PlusWnd.GetControlText("LanguageFile");
			g_oSettings.NOW_PLAYING = PlusWnd.Button_IsChecked("MPSM");
			g_oSettings.NOW_PLAYING_FORMAT = PlusWnd.GetControlText("MPSMFormat");
			g_oSettings.PLAYER_ALPHA = 255-PlusWnd.SendControlMessage("SldTransPlayer", 0x400, 0, 0);
			g_oSettings.PLAYLIST_ALPHA = 255-PlusWnd.SendControlMessage("SldTransPlaylist", 0x400, 0, 0);
			
			if(WL_PLAYER){
				WL_PLAYER.ImageElmt_SetImageFile("BGImg", g_oSettings.BACKGROUND_IMAGE?"\\" + g_oSettings.BACKGROUND_IMAGE:"BGImg.png");
				WL_SetAlpha(WL_PLAYER.handle, g_oSettings.PLAYER_ALPHA);
				}
			
			if(WL_PLAYLIST)
				WL_SetAlpha(WL_PLAYLIST.handle, g_oSettings.PLAYLIST_ALPHA);
			
			if(!g_oSettings.NOW_PLAYING)
				WL_SetNowPlaying(0);
			
			WL_LoadLanguage(g_oSettings.LANGUAGE);
			PlusWnd.Close(1);
		}
	}

function OnWLMiniEvent_CtrlClicked(PlusWnd, ControlId){
	switch(ControlId){
		case "BtnPlay":WL_Controls(WL_PLAY);break;
		case "BtnPause":WL_Controls(WL_PAUSE);break;
		case "BtnStop":WL_Controls(WL_STOP);break;
		case "BtnNext":WL_Controls(WL_NEXT);break;
		case "BtnPrev":WL_Controls(WL_PREV);break;
		case "ChkLoop":WL_Controls(WL_LOOP);break;
		case "ChkShuffle":WL_Controls(WL_SHUF);break;
		case "BtnPL":WL_ShowPlaylist();break;
		case "BtnOpen":
			var OFD = OpenFileDialog("Open Music", WL_PLAYER.handle, "Music Files|*.mp3;*.wma;*.wpl;*.m3u;*.wav|All Files|*.*|");
			for(i in OFD)
				g_hWMP.currentPlaylist.appendItem(g_hWMP.newMedia(OFD[i]));
			if(OFD.length)
				WL_Controls(WL_PLAY, g_hWMP.currentPlaylist.count - OFD.length);
		break;
		}
	}

function OnWLMiniEvent_MessageNotification(PlusWnd, Message, wParam, lParam){
	switch(Message){
		case 0x114:
			switch(lParam){
				case PlusWnd.GetControlHandle("position"):g_hWMP.controls.currentPosition = PlusWnd.SendControlMessage("position", 0x400, 0, 0);break;
				case PlusWnd.GetControlHandle("volume"):g_oSettings.VOLUME = g_hWMP.settings.volume = PlusWnd.SendControlMessage("volume", 0x400, 0, 0);break;
				}
		break;
		case 0x233:
			var lpszFile = Interop.Allocate(10242);
			var i = Interop.Call("shell32", "DragQueryFileW", wParam, 0xFFFFFFFF, 0, 0);
			for(x=0;x<i;x++){
				Interop.Call("shell32", "DragQueryFileW", wParam, x, lpszFile, 10242);
				g_hWMP.currentPlaylist.appendItem(g_hWMP.newMedia(lpszFile.ReadString(0)));
				}
			WL_Controls(WL_PLAY, i);
		break;
		}
	}

function OnplistEvent_MessageNotification(PlusWnd, Message, wParam){
	var lpszFile = Interop.Allocate(10242);
	var i = Interop.Call("shell32", "DragQueryFileW", wParam, 0xFFFFFFFF, 0, 0) - 1;
	do{
		Interop.Call("shell32", "DragQueryFileW", wParam, i, lpszFile, 10242);
		g_hWMP.currentPlaylist.appendItem(g_hWMP.newMedia(lpszFile.ReadString(0)));
		}while(i--);
	}

function OnplistEvent_LstViewRClicked(PlusWnd, ControlId, ItemIdx){
	if(ItemIdx == -1)
		return;
	var hMenu = Interop.Call("user32", "CreatePopupMenu");
	Interop.Call("user32", "AppendMenuW", hMenu, 0, 101, WL_GetLang("PLAYLIST_MENUPLAY"));
	Interop.Call("user32", "AppendMenuW", hMenu, 0x800, 0, 0);
	Interop.Call("user32", "AppendMenuW", hMenu, 0, 102, WL_GetLang("PLAYLIST_MENUMOVEUP"));
	Interop.Call("user32", "AppendMenuW", hMenu, 0, 103, WL_GetLang("PLAYLIST_MENUMOVEDOWN"));
	Interop.Call("user32", "AppendMenuW", hMenu, 0x800, 0, 0);
	Interop.Call("user32", "AppendMenuW", hMenu, 0, 104, WL_GetLang("PLAYLIST_MENUREMOVE"));
	Interop.Call("user32", "GetCursorPos", POINTAPI = Interop.Allocate(8));
	ProcessMenuItem(PlusWnd, ItemIdx, Interop.Call("user32", "TrackPopupMenu", hMenu, 0x0100, POINTAPI.ReadDWORD(0), POINTAPI.ReadDWORD(4), 0, PlusWnd.handle, 0));
	}

function ProcessMenuItem(PlusWnd, ItemIdx, MenuId){
	if(MenuId){
		var oCurPlaylist = g_hWMP.currentPlaylist;
		var iPlaylistId = PlusWnd.LstView_GetItemData("playlist", ItemIdx);
		switch(MenuId){
			case 101:WL_Controls(WL_PLAY, iPlaylistId);break;
			case 102:case 103:oCurPlaylist.moveItem(iPlaylistId, MenuId==102?iPlaylistId<1?oCurPlaylist.count-1:iPlaylistId-1:iPlaylistId==oCurPlaylist.count-1?0:iPlaylistId+1);break;
			case 104:WL_DeletePlaylist(ItemIdx);break;
			}
		}
	}

function OnEvent_MenuClicked(MenuItemId){eval(MenuItemId + "()");}

function OnEvent_ChatWndSendMessage(ChatWnd, Message){
	var aCmd = Message.toLowerCase().split(" ");
	switch(aCmd[0]){
		case "/wlmini":
			switch(aCmd[1]){
				case "play":WL_Controls(WL_PLAY, aCmd[2]||-1);break;
				case "pause":WL_Controls(WL_PAUSE);break;
				case "stop":WL_Controls(WL_STOP);break;
				default:WL_ShowPlayer();
				}
			return "";
		case "/plist":WL_ShowPlaylist();return "";
		}
	}